package com.iterator.service.wechat.lyh;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.wechat.lyh.WxDataFootprintVO;
import com.iterator.bean.wechat.lyh.WxFootprintVO;
import com.iterator.mapper.wechat.lyh.WxFootprintMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxFootprintServiceImpl implements WxFootprintService {

    @Autowired
    WxFootprintMapper footprintMapper;

    @Override
    public WxDataFootprintVO getFootprintList(String username, Integer page, Integer size) {
        //开启分页
        PageHelper.startPage(page, size);

        //查询
        List<WxFootprintVO> footprintVOS =
                footprintMapper.selectFootprintListByUsername(username);

        //分页前的数据量
        PageInfo<WxFootprintVO> pageInfo = new PageInfo<>(footprintVOS);
        Long total = pageInfo.getTotal();

        WxDataFootprintVO dataVO = new WxDataFootprintVO();
        dataVO.setFootprintList(footprintVOS);
        //这是为了获得total/size的向上取整值,比如8/10,10/10
        if (total % size != 0) total += 10;
        dataVO.setTotalPages(total/size);

        return dataVO;
    }

    @Override
    public Integer deleteFootprint(String username, Integer id) {
        //先查这样的用户足迹是否存在,如果不查询,且这样的用户足迹不存在update更新会报错
        Integer total = footprintMapper.selectFootprintByUsername(username, id);
        if (total == 0) {
            return total;
        }
        //如果存在再执行更新操作
        footprintMapper.deleteFootprintById(id);
        return total;
    }
}
